package cn.fastdfs.util;

import java.io.FileNotFoundException;
import java.io.IOException;

import org.csource.common.NameValuePair;
import org.csource.fastdfs.ClientGlobal;
import org.csource.fastdfs.StorageClient1;
import org.csource.fastdfs.StorageServer;
import org.csource.fastdfs.TrackerClient;
import org.csource.fastdfs.TrackerServer;

public class FastDFSUtil {
	
	
	public static String uploadFile(byte[] file,String name,String ext,int size) {
		
		String val = null;
		TrackerClient tracker = null;
		TrackerServer trackerServer = null;
		try{
			tracker = new TrackerClient();  
			trackerServer = tracker.getConnection();  
			StorageServer storageServer = null;  
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			NameValuePair[] metaList = new NameValuePair[3];  
			metaList[0] = new NameValuePair("fileName", name);  
			metaList[1] = new NameValuePair("fileExtName", ext);  
			metaList[2] = new NameValuePair("fileLength", size+"");  
			return client.upload_file1(file, ext, metaList);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				trackerServer.close();
			} catch (IOException e) {
			}
		}
	    return val;
	}
	
	public static byte[] downloadFile(String fileId){
		byte[] a_b = null;
		TrackerClient tracker = null;
		TrackerServer trackerServer = null;
		try{
			tracker = new TrackerClient();  
			trackerServer = tracker.getConnection();  
			StorageServer storageServer = null;  
			StorageClient1 client = new StorageClient1(trackerServer, storageServer);
			return client.download_file1(fileId);
			
		}catch(Exception e){
			e.printStackTrace();
		}finally {
			try {
				trackerServer.close();
			} catch (IOException e) {
			}
		}
		return a_b;
	}
}
